package test;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * describe:
 *
 * @author helloworldyu
 * @date 2018/11/12
 */
public class HelloWorldCommand extends HystrixCommand<String> {
	private String commandName;


	public HelloWorldCommand(String commandName) {
		super(HystrixCommandGroupKey.Factory.asKey(HelloWorldCommand.class.getSimpleName()));
		this.commandName = commandName;
	}

	@Override
	protected String run() throws Exception {
		return commandName + " ok";
	}


	public static void main(String[] args) {
//		testSync();
		testAsync();
	}

	/**
	 * 	简单的同步执行测试
	 */
	private static void testSync(){
		HelloWorldCommand command = new HelloWorldCommand("helloworld sync");
		String result = command.execute();
		System.out.println(result);
	}

	private static void testAsync() {
		Future<String> future = new HelloWorldCommand("async").queue();
		try {
			String result = future.get();
			System.out.println(result);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}

	}
}


